#include <REGX52.H>
#include "I2C.h"


#define AT24C02_ADDRESS 0xA0 //AT24C02写的地址

/**********************************************************************************/

/**
  *@brief  主机向AT24C02发送数据
  *@param  WordAddress 写入的字地址
  *@param  Data 写入的数据
  *@retval 无
  */
void AT24C02_WriteByte(unsigned char WordAddress,Data)
{
	unsigned char Ack = 0;//用于接收从机发送的应答
	I2C_Start();//通信开始
	
	I2C_SentByte(AT24C02_ADDRESS);//写入从机地址和Write位
	Ack = I2C_ReceiveAck();//从机发送应答
	
	I2C_SentByte(WordAddress);//写入字地址和..
	Ack = I2C_ReceiveAck();//主机接收应答
	
	I2C_SentByte(Data);//写入数据
	Ack = I2C_ReceiveAck();//从机发送应答

	I2C_Stop();//通信结束
}

/**********************************************************************************/

/**
  *@brief  主机接收AT24C02发送的数据
  *@param  WordAddress 需要获取的数据的地址
  *@retval 需要获取的数据
  */
unsigned char AT24C02_ReadByte(unsigned char WordAddress)
{
	unsigned char Data = 0x00;//用于获取从机发送的数据
	unsigned char Ack = 0;//用于接收从机发送的应答
	
	I2C_Start();//通信开始
	
	I2C_SentByte(AT24C02_ADDRESS);//写入从机地址和Write位
	Ack = I2C_ReceiveAck();//主机接收应答
	
	I2C_SentByte(WordAddress);//写入字地址和..
	Ack = I2C_ReceiveAck();//从机发送应答

	
	I2C_Start();//通信开始
	
	I2C_SentByte(AT24C02_ADDRESS | 0x01);//写入从机地址和Read位
	Ack = I2C_ReceiveAck();//主机接收应答

	Data = I2C_ReceiveByte();//主机接收数据
	I2C_SendAck(1);//主机选择发送应答
	
	I2C_Stop();//通信结束	
	return Data;//返回数据
}

/**********************************************************************************/


















